fix(quota): make InMemoryQuotaService constructor public so it resolves under DI#1330
Open
marcelo-maciel wants to merge 1 commit into
Open
Conversation
…es under DI
With QuotaOptions.Enabled=true and no Redis configured, AddHeroQuotas registers
InMemoryQuotaService as the IQuotaService. Its constructor was internal, and the
default DI container only considers public constructors, so QuotaEnforcementMiddleware
could not resolve the service per request -- turning every authenticated request, the
login included, into a 500 ("A suitable constructor for type '...InMemoryQuotaService'
could not be located"). The host disables ValidateOnBuild, so the failure surfaced at
request time instead of startup. RedisQuotaService already had a public constructor,
which is why only the in-memory (dev/test) path was affected.
Add Framework.Tests coverage that resolves IQuotaService from a scope with quota both
enabled and disabled, guarding the exact seam that broke.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Turning quota enforcement on brings the whole app down. With
QuotaOptions.Enabled=trueand no Redis configured, every authenticated request returns 500 — the login (/api/token) included. The "quota on" path ships disabled by default (QuotaOptions.Enabled=falsein every appsettings) and appears never to have been exercised by a test, so the regression went unnoticed.Root cause
AddHeroQuotas, when enabled without Redis, registersInMemoryQuotaServiceas theIQuotaServiceimplementation. Its constructor wasinternal, and the default DI container only considers public constructors.QuotaEnforcementMiddlewareresolvesIQuotaServiceper request, so resolution threw:The host disables
ValidateOnBuild, so this surfaced at request time rather than at startup — hence a running app that 500s on the first request through the pipeline.RedisQuotaServicealready had apublicconstructor, which is why only the in-memory (dev/test) path was affected.Captured live via
QuotaOptions__Enabled=true dotnet test ... --filter BillingDomainEdgeTests: the loginPOST /api/v1/identity/token/issuereturned the exact exception above in the response body before the fix, and 6/6 pass after.Fix
Make the
InMemoryQuotaServiceconstructorpublic, matching its siblingRedisQuotaService. One-word change; no behavioural change beyond making the type constructible by the container.Test
Adds
Framework.Tests/Quota/QuotaExtensionsTeststhat resolvesIQuotaServicefrom a scope (mirroring how the middleware resolves it) with quota both enabled and disabled. This pins the exact seam that broke — registration alone did not catch it becauseValidateOnBuildis off. The enabled test reproduces theInvalidOperationExceptionagainst the pre-fix code and passes after.Verification
dotnet test src/Tests/Framework.Tests— 114/114 pass.QuotaOptions__Enabled=true dotnet test ... --filter BillingDomainEdgeTests— 6/6 pass (2 failed before the fix).Out of scope (separate follow-up)
StorageBytesoverage is hard-capped:CheckAndRecordAsyncrolls back past the limit andQuotaMeteredStorageServicethrows 507, soUsednever exceedsLimitandUsageSnapshot.Overagestays 0 — storage overage never bills. A soft-cap mode (allow past the limit + record, soMonthlyInvoiceJobcan billUsed - Limit) is worth a separate issue/PR.